/**
 * Created by WT on 2017/4/5.
 */

$(function () {
    var PROJECT_SPECIALTY = CONSTANT.DictionaryTypeDefConstants.PROJECT_SPECIALTY;//项目专业类别PROJECT_SPECIALTY
    var PROJECT_OPERATIONSTATE = CONSTANT.DictionaryTypeDefConstants.PROJECT_OPERATIONSTATE;//项目当前状态
    queryComboData(PROJECT_SPECIALTY, 'querySpecialtye');
    queryComboData(PROJECT_OPERATIONSTATE, 'queryOperationState');
    //阶段信息切换事件
    if (safeAcceptance){
        $('#ttS').tabs('select', 3);
    }
    if(safeCommissioning){
        $('#ttS').tabs('select', 2);
    }
    if(safeDesign){
        $('#ttS').tabs('select', 1);
    }
    if(safeFSD){
        $('#ttS').tabs('select', 0);
    }

    //设计单位，来自承包商
    $('#design_designer').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                var me = this;
                WindowManager.openLayer({
                    type: 2,
                    title: '选择服务机构',
                    area: ['850px', '70%'],
                    content: ROOT_PATH + '/contractor/contractorInfo/simpleList.shtml',
                    callBack:function(data){
                        $("#design_designer").textbox('setText',data[0].name);
                        $("#design_designerId").val(data[0].id);
                        
                    }
                });
            }
        }]
    });

    $('#btn_save1').on('click', function () {
        var content=ROOT_PATH + '/threeSimultaneities/safe/safeFSD/add.shtml';
        saveSafeStage('safeFSD',content,1);
    });
    $('#btn_save2').on('click', function () {
        var content=ROOT_PATH + '/threeSimultaneities/safe/safeDesign/add.shtml';
        saveSafeStage('safeDesign',content,2);
    });
    $('#btn_save3').on('click', function () {
        var content=ROOT_PATH + '/threeSimultaneities/safe/safeCommissioning/add.shtml';
        saveSafeStage('safeCommissioning',content,3);
    });
    $('#btn_save4').on('click', function () {
        var content=ROOT_PATH + '/threeSimultaneities/safe/safeAccept/add.shtml';
        saveSafeStage('safeAcceptance',content,'tabs');
    });
});

/**
 * 保存阶段信息
 */
function saveSafeStage(formID, content,tab) {
    var formId = '#' + formID;
    var url = content;
    $(formId).form('submit', {
        url: url,
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                if (tab!='tabs'){
                    $('#ttS').tabs('enableTab', tab);
                }
                layer.confirm('保存成功,是否关闭本页面？', function () {
                    WindowManager.closeLayer({
                        returnValue: true
                    });
                });
                /*layer.msg('保存成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                });*/
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
/**
 * 查询下拉列表
 * @param code
 * @param queryId
 */
function queryComboData(code, queryId) {
    var pageId = '#' + queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            var multiple;
            //项目专业可多选
            if(queryId=='querySpecialtye'){
                multiple=true;
            }else{
                multiple=false;
            }
            $(pageId).combobox({
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                multiple:multiple,
                hasDownArrow:false
            });
        }
    });
}
